﻿using System.Threading.Tasks;
using FluentEmail.Core;
using FluentEmail.Core.Models;
using FluentEmail.Smtp;
using NiuX.Utils.Utils.Models;

namespace NiuX.Utils.Utils;

public static class FluentEmailHelper
{
    public static void SetDefaultSender(EmailOptions options)
    {
        Email.DefaultSender = new SmtpSender(options.CreateSmtpClient());
    }

    public static SendResponse Send(EmailSendModel model)
    {
        return Email
            //发送人
            .From(model.FromEmailAddress)
            //收件人
            .To(model.ToEmailAddress)
            //抄送人
            .CC(model.CCEmailAddress)
            //邮件标题
            .Subject(model.Subject)
            //邮件内容
            // .Body("邮件内容");
            .Body(model.Body, model.IsHtmlBody).Send();
    }

    public static Task<SendResponse> SendAsync(EmailSendModel model)
    {
        return Email
            //发送人
            .From(model.FromEmailAddress)
            //收件人
            .To(model.ToEmailAddress)
            //抄送人
            .CC(model.CCEmailAddress)
            //邮件标题
            .Subject(model.Subject)
            //邮件内容
            // .Body("邮件内容");
            .Body(model.Body, model.IsHtmlBody).SendAsync();
    }
}